﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.Calendar;
using Google.GData.Client;

namespace PlanoAulaHelper
{
    public partial class ImportarFeriadosDoGoogleForm : FormEdicao
    {
        public ImportarFeriadosDoGoogleForm()
        {
            InitializeComponent();
        }

        private void btnImportar_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbxUrl.Text))
            {
                return;
            }
            EventFeed myResultsFeed = ObterDatas();
            int qtdAtualizado = 0;
            int qtdNovo = 0;
            using (dbContext db = new dbContext())
            {
                foreach (EventEntry feedEntry in myResultsFeed.Entries)
                {
                    if (feedEntry.Times.Count == 0)
                    {
                        continue;
                    }
                    DateTime dataFeriado = feedEntry.Times[0].StartTime;

                    var dia = (from d in db.DiaNaoUtils
                               where d.Data == dataFeriado
                               select d).FirstOrDefault();
                    if (dia == null || !cbxSobrescreverExistente.Checked)
                    {
                        db.DiaNaoUtils.InsertOnSubmit(new DiaNaoUtil { Data = dataFeriado.Date, Descricao = feedEntry.Title.Text });
                        qtdNovo++;
                    }
                    else
                    {
                        dia.Data = dataFeriado.Date;
                        dia.Descricao = feedEntry.Title.Text;
                        qtdAtualizado++;
                    }
                    db.SubmitChanges();
                }
            }
            MessageBox.Show(string.Format("Calendário importado[Novos:{0} | Atualizados: {1}.", qtdNovo, qtdAtualizado));
        }

        private EventFeed ObterDatas()
        {
            CalendarService service = new CalendarService("exampleCo-exampleApp-1");
            if (!string.IsNullOrEmpty(tbxEmail.Text))
                service.setUserCredentials(tbxEmail.Text, tbxSenha.Text);
            else
                service.Credentials = null;

            string feedUri = tbxUrl.Text;

            EventQuery myQuery = new EventQuery(feedUri);
            myQuery.StartTime = dtpDe.Value;
            myQuery.EndTime = dtpAte.Value;

            EventFeed myResultsFeed = service.Query(myQuery) as EventFeed;
            return myResultsFeed;
        }

        private void ImportarFeriadosDoGoogleForm_Load(object sender, EventArgs e)
        {
            dtpDe.Value = new DateTime(DateTime.Today.Year, 01, 01);
            dtpAte.Value = new DateTime(DateTime.Today.Year, 12, 31);
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
